# Copyright (c) 2022, hzmes and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document
from frappe.model.mapper import get_mapped_doc
from frappe.utils import getdate, today
from frappe.utils.file_manager import remove_file

class SRMSampleAdmit(Document):
	def autoname(self):
		if self.test_no:
			self.name = self.test_no


	def before_insert(self):
		if not self.test_no:
			current_test_no = "B"
			current_test_no += "".join(today().split("-"))
			series = frappe.db.get_value('SRM Naming Series',
								{'doctype_name': self.doctype, 'date': today()},
								['name', 'current_series'], as_dict=True)
			if series:
				current_test_no += str(series.current_series).zfill(3)
			else:
				frappe.get_doc({'doctype': 'SRM Naming Series', 'doctype_name': self.doctype, 'date': today()}).insert(ignore_permissions=True)
				current_billno += "001"
			self.test_no = current_test_no
			frappe.db.set_value('SRM Naming Series', series.name, 'current_series', series.current_series + 1)


	@frappe.whitelist()
	def workflow_update(self, filed_user, filed_date, user, cur_date=None, filed_username=None):
		frappe.db.set_value(self.doctype, self.name, filed_user, user)
		if filed_username:
			frappe.db.set_value(self.doctype, self.name, filed_username, frappe.get_doc("User", user).username)
		frappe.db.set_value(self.doctype, self.name, filed_date, cur_date)
		self.reload()

	@frappe.whitelist()
	def my_set_value(self, filed_target, filed_value=None):
		frappe.db.set_value(self.doctype, self.name, filed_target, filed_value)
		self.reload()

	# 批量更新字段,以dict传递
	@frappe.whitelist()
	def field_update(self, dict):
		for key, value in dict.items():
			if key in self.as_dict().keys():
				frappe.db.set_value(self.doctype, self.name, key, value)
		self.reload()


	# 更新字段,以dict传递,忽略权限
	@frappe.whitelist()
	def inspect_receive(self):
		self.quality_confirm_user = frappe.session.user_fullname
		self.save(ignore_permissions=True)
		self.reload()


	# 生成[试产申请单]
	@frappe.whitelist()
	def make_trial_production(self, source_name, target_doc=None):
		need_date = today()
		applicant = frappe.session.user

		doc = get_mapped_doc(
			"SRM Sample Admit",
			source_name,
			{
				"SRM Sample Admit": {
					"doctype": "SRM Trial Production",
					"field_map": {"name":"sample_admit"},
					"validation": {},
				},
				# "postprocess": update_target,
				# "condition": lambda doc: abs(doc.received_qty) < abs(doc.qty),
			},
			target_doc,
		)
		doc.bill_type = "选择样品承认书"
		doc.need_date = need_date
		doc.applicant = applicant
		doc.insert()
		self.db_set('is_trial_production', 1)
		return doc


	# 附件修改 2023-04-16
	@frappe.whitelist()
	def attach_set_value(self, file_url_new, attached_to_field):
		frappe.db.set_value(self.doctype, self.name, attached_to_field, file_url_new)
		self.reload()


@frappe.whitelist()
def remove_attach(file_name):
	"""remove attachment""" 	
	# fid = frappe.form_dict.get("fid")
	# file_name = frappe.form_dict.get("file_name")
	frappe.delete_doc("File", file_name)